import {
  getNewsInfo, getNewsInfoByNewsId
} from '@/api/news'

const state = {
  news: [],
  news_total: 0
}

const mutations = {

  SET_NEWS(state, data) {
    state.news = data.records
    state.news_total = data.total
  }
}

const actions = {

  getNewsInfo({commit}, data) {
    return new Promise((resolve, reject) => {
      getNewsInfo(data).then((resp) => {
        if (resp.data.code === 1) {
          resolve(true)
          commit("SET_NEWS", resp.data.data)
        } else if (resp.data.code === 0) {
          reject(resp.data.msg)
        }
        reject("error")
      }).catch((error) => {
        reject(error)
      })
    })
  },

  getNewsInfoByNewsId({commit}, id) {
    return new Promise((resolve, reject) => {
      getNewsInfoByNewsId(id).then((resp) => {
        if (resp.data.code === 1) {
          resolve(resp)
        }
        reject("error")
      }).catch((error) => {
        reject(error)
      })
    })
  },

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
